package com.tj.jinzao.presenter;

import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.business.session.emoji.MoonUtil;
import com.tj.jinzao.R;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.common.Settings;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.utils.ToastUtils;

import java.util.HashMap;
import java.util.List;

/**
 * 日记，文章列表处理
 * Created by wjy 2018/12/10.
 */
public class PublicArticlePresenter extends BasePresenter<IUploadView<String>> {

    public PublicArticlePresenter(IUploadView<String> view) {
        super(view);
    }

    /**
     * 发布日记
     */
    public void publicDaily(String content, String imageUrls, boolean video, String videoUrl) {
        videoUrl = videoUrl == null ? "" : videoUrl;
        HashMap<String, Object> map = new HashMap();
        map.put("uid",UserManager.getInstance().getUser().getId());
        map.put("content",content);
        map.put("imgurls",imageUrls);
        map.put("type",1);
        map.put("video",video);
        map.put("videoUrl",videoUrl);
        String city = Settings.getString(mView.getContext(), Settings.KEY_PREF_CITY, "");
        if (!TextUtils.isEmpty(city)) {
            map.put("city", city);
        }

        addSubscription(mApiService.publicArticle(map), new SubscriberCallBack<Article>(mView.getContext()) {
            @Override
            protected void onSuccess(Article article) {
                if (article != null) {
                    if (mView != null) {
                        ToastUtils.showToast(mView.getContext(), "日记发布成功");
                        mView.getContext().finish();
                    }
                }
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    /**
     * 发布话题
     */
    public void publicTopic(String title, String content, String tag, String imageUrls, boolean video, String videoUrl) {
        videoUrl = videoUrl == null ? "" : videoUrl;
        HashMap<String, Object> map = new HashMap();
        map.put("uid",UserManager.getInstance().getUser().getId());
        map.put("content", content);
        map.put("imgurls", imageUrls);
        map.put("title",title);
        map.put("tag",tag);
        map.put("type",2);
        map.put("video",video);
        map.put("videoUrl", videoUrl);
        String city = Settings.getString(mView.getContext(), Settings.KEY_PREF_CITY, "");
        if (!TextUtils.isEmpty(city)) {
            map.put("city", city);
        }
        Log.w("数据请求","ssscccc"+map.toString());
        addSubscription(mApiService.publicArticle(map), new SubscriberCallBack<Article>(mView.getContext()) {
            @Override
            protected void onSuccess(Article article) {
                if (article != null) {
                    if (mView != null) {
                        ToastUtils.showToast(mView.getContext(), "发布成功");
                        mView.getContext().finish();
                    }
                }
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    public void setContentView(LayoutInflater inflater, String content, List<String> paths, boolean isVideo, LinearLayout linearLayout, View.OnClickListener listener) {
        linearLayout.removeAllViews();
        if (TextUtils.isEmpty(content)) {
            View view = inflater.inflate(R.layout.item_public_edittext, null);
            EditText editText = view.findViewById(R.id.et_content);
            editText.setHint("输入文字和图片…");
            linearLayout.addView(view);
            return;
        }
        String newContent = new String(content);
        //循环根据内容添加view
        int imageIndex = 0;
        do {
            View view = null;
            if (newContent.startsWith(Constant.ARTICLE_TAG)) {
                view = inflater.inflate(R.layout.item_public_image, null);
                if (isVideo) view.findViewById(R.id.iv_play).setVisibility(View.VISIBLE);
                ImageView imageView = view.findViewById(R.id.iv_image);
                String path = paths.get(imageIndex);
                Glide.with(mView.getContext()).load(path).into(imageView);
                View delete = view.findViewById(R.id.iv_delete);
                delete.setTag(R.id.data, path);
                delete.setOnClickListener(listener);
                imageView.setTag(R.id.data, path);
                imageView.setOnClickListener(listener);
                newContent = newContent.substring(Constant.ARTICLE_TAG.length(), newContent.length());
                imageIndex++;
            } else {
                int cutIndex = newContent.indexOf(Constant.ARTICLE_TAG);
                view = inflater.inflate(R.layout.item_public_edittext, null);
                EditText editText = view.findViewById(R.id.et_content);
                if (cutIndex != -1) {
                    String cutContent = newContent.substring(0, cutIndex);
                    MoonUtil.identifyFaceExpression(NimUIKit.getContext(), editText, cutContent, ImageSpan.ALIGN_BOTTOM);
                    editText.setSelection(cutContent.length());
                    newContent = newContent.substring(cutIndex, newContent.length());
                } else {
                    String cutContent = new String(newContent);
                    editText.setText(cutContent);
                    newContent = "";
                }
            }
            if (view != null) linearLayout.addView(view);
            if (view instanceof RelativeLayout && TextUtils.isEmpty(newContent)) {
                View view1 = inflater.inflate(R.layout.item_public_edittext, null);
                linearLayout.addView(view1);
            }
        } while (newContent.length() > 0);
    }

    public void setShowContentView(LayoutInflater inflater, String content, List<String> paths, boolean isVideo, LinearLayout linearLayout, View.OnClickListener listener) {
        linearLayout.removeAllViews();
        if (TextUtils.isEmpty(content)) return;
        String newContent = new String(content);
        //循环根据内容添加view
        int imageIndex = 0;
        do {
            View view = null;
            if (newContent.startsWith(Constant.ARTICLE_TAG)) {
                view = inflater.inflate(R.layout.item_public_image, null);
                if (isVideo) view.findViewById(R.id.iv_play).setVisibility(View.VISIBLE);
                ImageView imageView = view.findViewById(R.id.iv_image);
                String path = paths.get(imageIndex);
                Glide.with(mView.getContext()).load(path).into(imageView);
                view.findViewById(R.id.iv_delete).setVisibility(View.GONE);
                newContent = newContent.substring(Constant.ARTICLE_TAG.length(), newContent.length());
                imageIndex++;
            } else {
                int cutIndex = newContent.indexOf(Constant.ARTICLE_TAG);
                view = inflater.inflate(R.layout.item_public_textview, null);
                TextView textView = view.findViewById(R.id.tv_content);
                if (cutIndex != -1) {
                    String cutContent = newContent.substring(0, cutIndex);
                    MoonUtil.identifyFaceExpression(NimUIKit.getContext(), textView, cutContent, ImageSpan.ALIGN_BOTTOM);
                    newContent = newContent.substring(cutIndex, newContent.length());
                } else {
                    String cutContent = new String(newContent);
                    MoonUtil.identifyFaceExpression(NimUIKit.getContext(), textView, cutContent, ImageSpan.ALIGN_BOTTOM);
                    newContent = "";
                }
            }
            if (view != null) linearLayout.addView(view);
        } while (newContent.length() > 0);
    }

    @Override
    protected void onUploadSuccess(String localPath, String response) {
        if (mView != null)
            mView.onUploadSuccess(localPath, response);

    }

    @Override
    protected void onUploadFailed(String localPath, ResultResponse response) {
        super.onUploadFailed(localPath, response);
    }
}
